const express = require("express")

const Router = express.Router()
const configSql = require("../../db/db");
// 酒店入住城市
Router.get("/get_city", (req, res) => {

    let sql = 'select city from hotel_list limit 0,100'
    let city_list = []
    configSql(sql).then(result => {
        for (let i = 0; i < result.length; i++) {
            if (!city_list.some(item => result[i].city == item.city)) {
                if (city_list.length < 30) {
                    city_list.push(result[i])
                }
            }
        }
        res.send({
            code: 0,
            msg: '获取成功',
            data: city_list
        })
    })
})


// 品牌连锁

Router.get("/get_hotel_list", (req, res) => {
    let hotel_list = []
    let sql = 'select bind from hotel_list limit 0,60'
    configSql(sql).then(result => {
        for (let i = 0; i < result.length; i++) {
            if (!hotel_list.some(item => result[i].bind == item.bind)) {
                if (hotel_list.length < 30) {
                    hotel_list.push(result[i])
                }
            }
        }
        res.send({
            code: 0,
            msg: '获取成功',
            data: hotel_list
        })
    })
})


// 热门入住城市
// (城市)
Router.get('/get_hotel_city', (req, res) => {
    let city_list = [{
        id: -1,
        city: '全部'
    }, {
        id: 0,
        city: '桂林'
    }, {
        id: 1,
        city: '安顺',
    }, {
        id: 2,
        city: '南宁'
    }, {
        id: 3,
        city: '北海'
    }, {
        id: 4,
        city: '承德'
    }, {
        id: 5,
        city: '丹东'
    }, {
        id: 6,
        city: '柳州'
    }, ]
    res.send({
        code: 0,
        msg: '获取成功',
        data: city_list
    })


})

// 酒店图片列表
Router.get('/get_hot_hotel_list', (req, res) => {

    let {
        city,
        type
    } = req.query
    let sql
    if (city == '全部') {
        sql = `select * from hotel_list where type=${type}`
    } else {
        sql = `select * from hotel_list where type=${type} and city='${city}'`
    }

    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: "获取成功",
            data: result
        })
    })

})


// 热门城市酒店
Router.get('/get_all_list', (req, res) => {

    let sql = `select * from hotel_list where type=11`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            data: result,
            msg: '获取成功'
        })
    })
})

// 查询酒店
Router.post('/select_hotel', (req, res) => {
    let {
        city,
        bind
    } = req.body
    if (!city || !bind) {
        res.send({
            code: 500,
            msg: '参数缺失'
        })
    }
    let sql = `select * from hotel_list where city like'%${city}%'and bind like'%${bind}%'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            data: result,
            msg: '获取成功'
        })
    })


})








// 导出路由
module.exports = Router;